#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash qt5-5.13.2.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar environment-modules libatomic

wget -t 10 -c -P $buildpath https://download.qt.io/archive/qt/5.13/5.13.2/single/qt-everywhere-src-5.13.2.tar.xz
tar xf $buildpath/qt-everywhere-src-5.13.2.tar.xz -C $buildpath && rm -rf $buildpath/qt-everywhere-src-5.13.2.tar.xz
cd $buildpath/qt-everywhere-src-5.13.2
./configure -openssl-linked -opensource -confirm-license --prefix=$installpath/qt5-5.13.2
make -j$(nproc)
make -j$(nproc) install

cat >$installpath/qt5-5.13.2/qt5_modulefiles<<EOF
#%Module1.0
conflict qt5
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set QTDIR \$pwd
setenv QTDIR \$QTDIR
prepend-path PATH \$QTDIR/bin
prepend-path MANPATH \$QTDIR/man
prepend-path LD_LIBRARY_PATH \$QTDIR/lib
EOF

rm -rf $buildpath/qt-everywhere-src-5.13.2